home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7459 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.pl1,comp.lang.c
  4. Subject: Re: PL/I and C
  5. Date: 26 Feb 1996 13:07:17 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4gt0d5$7v9@solutions.solon.com>
  8. References: <4gh5ru$eng@goanna.cs.rmit.EDU.AU> <AD536AAB9668B76CD@mcdialb09.it.luc.edu> <TANMOY.96Feb23175827@qcd.lanl.gov> <AD568E9F96689203B@mcdiala11.it.luc.edu>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <AD568E9F96689203B@mcdiala11.it.luc.edu>,
  12. Verne Arase <VArase@varase.it.luc.edu> wrote:
  13. >In C, a pointer to foo is practically the same as an array of foo. The only
  14. >difference (I believe) is that the array reserves storage, and can't have
  15. >its base address modified.
  16.  
  17. No, they're really quite different.
  18.  
  19. A pointer variable reserves storage - enough storage for an address.  An
  20. array variable reserves enough storage for some constant number of objects.
  21. An array value cannot be modified, although members of the array may; by
  22. contrast, pointers can be modified, as can the things they point to.
  23.  
  24. >If you have
  25.  
  26. >   foo *x;        /* declare a pointer to foo */
  27. >   foo y[10];     /* declare an array of foo */
  28.  
  29. >   x=y;           /* set x to the address of y */
  30.  
  31. >then x[n] and y[n] should have the same address and value.
  32.  
  33. Yes, but the third comment is misleading.  "x=y;" is setting x to the address
  34. of *y[0]*, not of y.  (*'s for emphasis, not dereferencing.)  The address of
  35. y is a thing of type pointer to array[10] of foo; the address of y[0], and
  36. the value you get when you refer to y in most expressions, is a thing
  37. of type pointer to foo.  (There are exceptions, most notably sizeof.  sizeof
  38. y will be 10 * sizeof(foo) - sizeof x will be sizeof(foo *).)
  39.  
  40. >A pointer to foo
  41. >can be dereferenced as *x, and this would have a value which is a foo. A
  42. >reference such as x[n] is actually equivalent to *(x+n) in C (in C when you
  43. >add an integer 'n' to a pointer, it returns the address of the 'n'th
  44. >element of that pointed-to type).
  45.  
  46. This is basically true; the [] operator (which is commutative) does
  47. exactly that.  Just remember that it works on arrays because an array decays
  48. into a pointer in an expression context.
  49.  
  50. (And no, there are *NO* C implementations in which [] is not commutative.
  51. There may be implementations of various languages in which it isn't, but they
  52. are not implementations of the C language.)
  53.  
  54. -s
  55. -- 
  56. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  57. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  58. FUCK the communications decency act.  Goddamned government.  [literally.]
  59. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  60.